package httpHandle

import (
	"github.com/TarsCloud/TarsGo/tars"
	"github.com/gin-gonic/gin"

	"github.com/wzh200x/webServer/constants"
	"github.com/wzh200x/webServer/httpHandle/service"
	"github.com/wzh200x/webServer/middleware/response"
)

func HandleAdOauth(c *gin.Context) {
	// 从请求参数中获取code
	success, err := response.GetResponseFuncs(c)
	var input service.AdOauthInfo
	if bindErr := c.ShouldBind(&input); bindErr != nil {
		err(constants.ErrUnknown, "无效的请求参数")
		return
	}
	// 打印请求参数
	tars.GetLogger("").Info("HandleAdOauth input: ", input)
	if handleErr := service.HandleAdOauth(&input); handleErr != nil {
		err(constants.ErrUnknown, handleErr.Error())
		return
	}
	success(&service.AdOauthInfo{})
}

func HadnleAdOathRefresh(c *gin.Context) {
	success, err := response.GetResponseFuncs(c)
	var input service.AdOauthRefreshInfo
	if bindErr := c.ShouldBind(&input); bindErr != nil {
		err(constants.ErrUnknown, "无效的请求参数")
		return
	}
	// 打印请求参数
	tars.GetLogger("").Info("HandleAdOauth input: ", input)
	if handleErr := service.HadleAdOathRefresh(&input); handleErr != nil {
		err(constants.ErrUnknown, handleErr.Error())
		return
	}
	success("OK")
}

func HandleAdOauthQuery(c *gin.Context) {
	success, err := response.GetResponseFuncs(c)
	var input service.AdOauthQueryInfo
	if bindErr := c.ShouldBind(&input); bindErr != nil {
		err(constants.ErrUnknown, "无效的请求参数")
		return
	}
	// 打印请求参数
	tars.GetLogger("").Info("HandleAdOauthQuery input: ", input)
	rsp, e := service.HandleAdOauthQuery(&input)
	if e != nil {
		err(constants.ErrUnknown, e.Error())
		return
	}
	success(rsp)
}
